<template>
  <el-dialog :visible.sync="dialogVisible" :width="type == 1? '680px':'460px'" :title="titleMap[type-1]" append-to-body
    @closed="$emit('closed')">

    <template v-if="type==1">
      <div class="sc-user-select">
        <div class="sc-user-select__left">
          <div class="sc-user-select__search">
            <el-input v-model="keyword" prefix-icon="el-icon-search" placeholder="搜索成员" @keyup.enter.native="search">
              <template #append>
                <el-button icon="el-icon-search" @click="search"></el-button>
              </template>
            </el-input>
          </div>
          <div class="sc-user-select__select">
            <div class="sc-user-select__tree" v-loading="showGrouploading">
              <el-scrollbar>
                <el-tree class="menu" ref="groupTree" :data="group" :node-key="'id'" draggable
                  :props="{ children: 'children' }" :expand-on-click-node="false" :current-node-key="groupId"
                  @node-click="groupClick">
                  <span class=" ml-10" slot-scope="{data}">
                    [{{ data.dept_code }}] {{ data.dept_name }}
                  </span>
                </el-tree>
              </el-scrollbar>
            </div>
            <div class="sc-user-select__user" v-loading="showUserloading">
              <div class="sc-user-select__user__list">
                <el-scrollbar ref="userScrollbar">
                  <el-tree class="menu" ref="userTree" :data="user" :node-key="'id'" :default-checked-keys="selectedIds"
                    show-checkbox check-on-click-node @check-change="userClick">
                  </el-tree>
                </el-scrollbar>
              </div>
              <footer>
                <el-pagination background layout="prev,next" small :total="total" :page-size="pageSize"
                  :currentPage.sync="currentPage" @current-change="paginationChange"></el-pagination>
              </footer>
            </div>
          </div>
        </div>
        <div class="sc-user-select__toicon">
          <i class="el-icon-arrow-right"></i>
        </div>
        <div class="sc-user-select__selected">
          <header>已选 ({{selected.length}})</header>
          <ul style="padding-left:0">
            <el-scrollbar>
              <li v-for="(item, index) in selected" :key="item.id">
                <span class="name">
                  <el-avatar size="small">
                    <i class="el-icon-user-solid"></i>
                  </el-avatar>
                  <label>{{item.name}}</label>
                </span>
                <span class="delete">
                  <el-button type="text" icon="el-icon-delete" circle size="small" @click="deleteSelected(index)">
                  </el-button>
                </span>
              </li>
            </el-scrollbar>
          </ul>
        </div>
      </div>
    </template>

    <template v-if="type==2">
      <div class="sc-user-select sc-user-select-role">
        <div class="sc-user-select__left">
          <div class="sc-user-select__select">
            <div class="sc-user-select__tree" v-loading="showGrouploading">
              <el-scrollbar>
                <el-tree class="menu" ref="groupTree" :data="role" :node-key="'id'" show-checkbox check-strictly
                  check-on-click-node :expand-on-click-node="false" :default-checked-keys="selectedIds"
                  @check-change="roleClick">
                  <span slot-scope="{ data }">
                    {{ data.label }}
                  </span>
                </el-tree>
              </el-scrollbar>
            </div>
          </div>
        </div>
        <div class="sc-user-select__toicon">
          <i class="el-icon-arrow-right"></i>
        </div>
        <div class="sc-user-select__selected">
          <header>已选 ({{selected.length}})</header>
          <ul style="padding-left:0">
            <el-scrollbar>
              <li v-for="(item, index) in selected" :key="item.id">
                <span class="name">
                  <label>{{item.name}}</label>
                </span>
                <span class="delete">
                  <el-button type="text" icon="el-icon-delete" circle size="mini" @click="deleteSelected(index)">
                  </el-button>
                </span>
              </li>
            </el-scrollbar>
          </ul>
        </div>
      </div>
    </template>

    <template #footer>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 认</el-button>
    </template>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      titleMap: ['人员选择', '角色选择'],
      dialogVisible: false,
      showGrouploading: false,
      showUserloading: false,
      keyword: '',
      groupId: '',
      pageSize: 10,
      total: 0,
      currentPage: 1,
      group: [],
      user: [],
      role: [],
      type: 1,
      selected: [],
      value: [],
    }
  },
  computed: {
    selectedIds() {
      return this.selected.map((t) => t.id)
    },
  },
  methods: {
    //打开赋值
    open(type, data) {
      this.type = type
      this.value = data || []
      this.selected = JSON.parse(JSON.stringify(data || []))
      this.dialogVisible = true
      if (this.type == 1) {
        this.getGroup()
      } else if (this.type == 2) {
        this.getRole()
      }
    },
    // //获取组织
    getGroup() {
      this.showGrouploading = true
      this.$request(this.$api.organize + 'getAllList')
        .then((r) => {
          this.group = r
          this.showGrouploading = false
        })
        .catch((e) => {
          this.showGrouploading = false
        })
    },
    // //获取用户
    getUser() {
      this.showUserloading = true
      var params = {
        page: this.currentPage,
        size: this.pageSize,
        deptid: this.groupId,
        queryString: this.keyword,
      }
      this.$request(this.$api.user + 'getlist', params).then((r) => {
        r.list.forEach((x) => {
          x.id = x.userid
          x.label = x.userid + ' ' + x.username
        })
        this.user = r.list
        this.total = r.total
        this.showUserloading = false
      })
    },
    //获取角色
    getRole() {
      this.showGrouploading = true
      this.$request(this.$api.role + 'getlist', { page: 1, size: 999 }).then((r) => {
        r.list.forEach((x) => {
          x.label = x.role_name
        })
        this.role = r.list
        this.showGrouploading = false
      })
    },
    // //组织点击
    groupClick(data) {
      this.keyword = ''
      this.currentPage = 1
      this.groupId = data.id
      this.getUser()
    },
    //用户点击
    userClick(data, checked) {
      if (checked) {
        this.selected.push({
          id: data.id,
          name: data.label,
        })
      } else {
        this.selected = this.selected.filter((item) => item.id != data.id)
      }
    },
    //用户分页点击
    paginationChange(v) {
      this.currentPage = v
      this.getUser()
    },
    //用户搜索
    search() {
      this.groupId = ''
      this.$refs.groupTree.setCurrentKey(this.groupId)
      this.currentPage = 1
      this.getUser()
    },
    //删除已选
    deleteSelected(index) {
      this.selected.splice(index, 1)
      if (this.type == 1) {
        this.$refs.userTree.setCheckedKeys(this.selectedIds)
      } else if (this.type == 2) {
        this.$refs.groupTree.setCheckedKeys(this.selectedIds)
      }
    },
    //角色点击
    roleClick(data, checked) {
      if (checked) {
        this.selected.push({
          id: data.id,
          name: data.label,
        })
      } else {
        this.selected = this.selected.filter((item) => item.id != data.id)
      }
    },
    //提交保存
    save() {
      this.value.splice(0, this.value.length)
      this.selected.map((item) => {
        this.value.push(item)
      })
      this.$emit('change', this.value)
      this.dialogVisible = false
    },
  },
}
</script>

<style scoped>
::v-deep .el-avatar--small {
  width: 24px;
  height: 24px;
  line-height: 24px;
}
.sc-user-select {
  display: flex;
}
.sc-user-select__left {
  width: 400px;
}
.sc-user-select__right {
  flex: 1;
}

.sc-user-select__search {
  padding-bottom: 10px;
  border: 1px solid #ffffff;
}

.sc-user-select__select {
  display: flex;
  border: 1px solid #e4e7ed;
  background: var(--el-color-white);
}
.sc-user-select__tree {
  width: 200px;
  height: 300px;
  border-right: 1px solid #e4e7ed;
  overflow-y: auto;
}
.sc-user-select__user {
  width: 200px;
  height: 300px;
  display: flex;
  flex-direction: column;
}
.sc-user-select__user__list {
  flex: 1;
  overflow: auto;
}
.sc-user-select__user footer {
  height: 36px;
  padding-top: 5px;
  border-top: 1px solid #e4e7ed;
}

.sc-user-select__toicon {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 10px;
}
.sc-user-select__toicon i {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #ccc;
  width: 20px;
  height: 20px;
  text-align: center;
  line-height: 20px;
  border-radius: 50%;
  color: #fff;
}

.sc-user-select__selected {
  height: 345px;
  width: 200px;
  border: 1px solid #e4e7ed;
  background: var(--el-color-white);
}
.sc-user-select__selected header {
  height: 43px;
  line-height: 43px;
  border-bottom: 1px solid #e4e7ed;
  padding: 0 15px;
  font-size: 12px;
}
.sc-user-select__selected ul {
  height: 300px;
  overflow: auto;
}
.sc-user-select__selected li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 5px 5px 15px;
  height: 38px;
}
.sc-user-select__selected li .name {
  display: flex;
  align-items: center;
}
.sc-user-select__selected li .name .el-avatar {
  background: #409eff;
  margin-right: 10px;
}
.sc-user-select__selected li .name label {
  font-weight: normal;
}
.sc-user-select__selected li .delete {
  display: none;
}
.sc-user-select__selected li:hover {
  background: var(--el-color-primary-light-9);
}
.sc-user-select__selected li:hover .delete {
  display: inline-block;
}

.sc-user-select-role .sc-user-select__left {
  width: 200px;
}
.sc-user-select-role .sc-user-select__tree {
  border: none;
  height: 343px;
}
.sc-user-select-role .sc-user-select__selected {
}

[data-theme='dark'] .sc-user-select__selected li:hover {
  background: rgba(0, 0, 0, 0.2);
}
[data-theme='dark'] .sc-user-select__toicon i {
  background: #383838;
}
</style>
